import config from "../../../../../config/config";

Component({
  properties: {
    product: {
      type: Object,
      value: {},
      observer(newVal, oldVal, changedPath) {
        if (Object.keys(newVal).length > 0) {
          this.sku()
          this.image()
          this.price()
        }
      }
    }
  },
  data: {
    image: '',
    price: 0,
    sku: []
  },
  methods: {
    sku() {
      if (this.properties.product.selected_sku_text) {
        this.setData({
          sku: this.properties.product.selected_sku_text
        })
      }
    },
    image() {
      const product = this.properties.product
      let url = ''
      if (product.sku && product.sku.image && product.sku.image.thumbnail_path) {
        url = product.sku.image.thumbnail_path
      } else if (product.mainImage && product.mainImage.thumbnail_path) {
        url = product.mainImage.thumbnail_path
      }
      this.setData({ image: config.app_base_url + url })
    },
    price() {
      const product = this.properties.product
      let price = +product.price
      let text = `￥${price}`
      if (product.sku) {
        price = +product.sku.price
        text = `￥${price}`
      }
      if (product.scoreCost) {
        text = `￥${+product.scoreCost.total} + ${+price}积分`
      }
      this.setData({ price: text})
    }
  }
});
